package com.lej.preview

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.lej.ai.preview.R
import com.lej.preview.data.PicInfo
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

class PreviewListActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        QMUIStatusBarHelper.translucent(this)
        val list = intent.getParcelableArrayListExtra<PicInfo>(PIC_LIST_TAG)
        val position = intent.getIntExtra(PIC_POSITION_TAG, 0)
        (window.decorView as? FrameLayout)?.let {

            it.setBackgroundColor(Color.BLACK)
            it.addView(ViewPager2(this).apply {
                layoutParams = ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                id = R.id.pre_viewpager
                transitionName = "PREVIEW${position}"
            })
            it.addView(TextView(this).apply {
                id = R.id.pre_text
                layoutParams = ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                setPadding(
                    paddingLeft,
                    ScreenUtil.getStatusBarHeight(this@PreviewListActivity) + 20,
                    paddingRight,
                    paddingBottom
                )
                gravity = Gravity.CENTER_HORIZONTAL
                setTextColor(Color.WHITE)
                textSize = 20f
            })
        }
        val viewPager: ViewPager2 = findViewById(R.id.pre_viewpager)

        if (list == null) {
            finish()
            return
        }
        viewPager.adapter = PrePagerAdapter(list, this)
        viewPager.setCurrentItem(position, false)
        findViewById<TextView>(R.id.pre_text)?.text = "${position + 1}/${list.size}"
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                findViewById<TextView>(R.id.pre_text)?.text = "${position + 1}/${list.size}"
                viewPager.transitionName = "PREVIEW${position}"
            }
        })
    }

    class PrePagerAdapter(private val data: List<PicInfo>, activity: AppCompatActivity) :
        FragmentStateAdapter(activity) {

        override fun createFragment(position: Int) = PreFragment(data[position])

        override fun getItemCount() = data.size
    }

    companion object {
        const val PIC_LIST_TAG = "pic_list_tag"
        const val PIC_POSITION_TAG = "pic_position_tag"

        fun start(
            activity: Activity,
            data: ArrayList<PicInfo>,
            position: Int = 0,
            view: View? = null
        ) {
            val intent = Intent(activity, PreviewListActivity::class.java).also {
                it.putParcelableArrayListExtra(PIC_LIST_TAG, data)
                it.putExtra(PIC_POSITION_TAG, position)
            }

            view?.let {
                val activityOptions =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(
                        activity,
                        Pair(it, "PREVIEW${position}")
                    )
                activity.startActivity(intent, activityOptions.toBundle())
            } ?: let {
                activity.startActivity(intent)
                activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }
}